package com.example.administrator.pandanotes;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Environment;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import org.litepal.LitePal;
import org.litepal.crud.DataSupport;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by 于博 on 2018-07-12.
 * 书籍类显示界面
 * 有添加按钮，点击跳转至书籍类添加界面
 * 点击列表项，跳转至书籍类更新界面
 * 长按列表项，长按出现删除对话框
 */
public class BookClassActivity extends AppCompatActivity {
    //变量***************************************************************************
    private FloatingActionButton fAbtnAdd = null;
    private ListView LvAllClass = null;
    private List<BookClass> ClassList = null; //数据列表资源
    private  User theUser = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_class);
        LitePal.initialize(this);
        //初始化及绑定********************************************************************
        try {
            FileReader r = new FileReader(Environment.getExternalStorageDirectory() + "/pandanotescookies.txt");
            BufferedReader reader = new BufferedReader(r);
            String id = reader.readLine();
            Log.d("UserName:",id);
            if(id != null && (!id.equals(""))){
                List<User> users = DataSupport.where("user_id = ?",id).find(User.class);
                theUser = users.get(0);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        fAbtnAdd = (FloatingActionButton) findViewById(R.id.fABtnClass); //书籍类添加跳转按钮
        LvAllClass = (ListView) findViewById(R.id.LvBookClass);//所有书籍类
        ClassList = new ArrayList<>();                          //书籍类
        initBookClass();                                          //初始化ListView数据
        //新建适配器
        final BookClassAdapter bookClassAdapter = new BookClassAdapter(BookClassActivity.this,R.layout.book_item,ClassList);
        //设置适配器
        LvAllClass.setAdapter(bookClassAdapter);
        //函数或事件****************************************************************
        fAbtnAdd.setOnClickListener(new View.OnClickListener() { //跳转至添加页面
            @Override
            public void onClick(View v) {                //浮动添加按钮点击事件，跳转至书籍类添加界面
                Intent intent =new Intent(BookClassActivity.this,BookClassAddActivity.class);
                startActivity(intent);
            }
        });
        LvAllClass.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {//列表项点击事件，跳转至书籍类更新界面
                BookClass bookClass = ClassList.get(position);       //得到点击的位置所对应的数据
                Log.d("bookClass:",bookClass.getClass_name()+bookClass.getBook_class_imageId());
                Log.d("BookClassActivity Id:",String.valueOf(id));
                Intent intent =new Intent(BookClassActivity.this,BookClassUpdateActivity.class);
                intent.putExtra("BookClass",bookClass);
                startActivity(intent);
            }
        });
        LvAllClass.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, final View view, final int position, final long id) {//列表项长按事件，长按出现删除对话框
                /********显示对话框**********/
                AlertDialog.Builder normalDialog =
                        new AlertDialog.Builder(BookClassActivity.this);
                normalDialog.setIcon(R.drawable.delete);
                normalDialog.setTitle(R.string.confirm_del);
                normalDialog.setMessage(R.string.are_your_sure);
                normalDialog.setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        TextView textView = (TextView) view.findViewById(R.id.fruit_name);
                        String string = textView.getText().toString();
                        Log.d("书籍名：",string);
                        int i = DataSupport.deleteAll(BookClass.class,"class_name=?",string);
                        Log.d("删除条数：",String.valueOf(i));
                        Log.d("删除书籍类：",String.valueOf(id));
                        Toast.makeText(BookClassActivity.this, "已删除", Toast.LENGTH_SHORT).show();
                        initBookClass();                         //adapter更新
                        bookClassAdapter.notifyDataSetChanged();//UI界面更新
                    }
                });
                normalDialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                normalDialog.show();
                return true;
            }
        });
    }
    private void  initBookClass()//列表初始化函数，添加书籍类信息至列表
    {
        List<BookClass> bookClass = DataSupport.findAll(BookClass.class); //得到书籍类表所有数据
        ClassList.clear();//清空
        for(BookClass bookClass1:bookClass)                                //每个数据项进行ListView的映射
        {
            BookClass bookClass2 = new BookClass(bookClass1.getClass_name(),bookClass1.getBook_class_imageId());//实例化，赋值
            ClassList.add(bookClass2);             //添加
        }
    }
    //返回键调用函数，进行上一页*************************************************************
    @Override
    public void onBackPressed()
    {
        this.finish(); //关闭当前Activity
        Intent intent = new Intent(BookClassActivity.this,MainActivity.class);
        startActivity(intent);
    }
}
